﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="/Style/JS/Aries.Loader.js"></script>
</head>
<body>
    <div class="content">
        <a>
            <input class="btn-lg" type="button" name="" id="btn_save" value="保存" />
        </a>
        <a>
            <input class="btn-lg" type="button" name="" id="btn_cancel" value="关闭" />
        </a>
    </div>
    <ul id="tree"></ul>
</body>
</html>

<script type="text/javascript">
    var roleID = AR.Utility.queryString("id");
    $(function () {
        $("#btn_save").click(function () {
            $.messager.progress({ title: "温馨提示" });
            var postData = { RoleID: roleID };
            if ($('#tree').tree('getRoot').checked) {
                postData.all = 1;
            } else {
                var arr = [], actions = {};
                var nodes = $("#tree").tree("getChecked");
                for (var i = 0; i < nodes.length; i++) {
                    var parentNode = $("#tree").tree("getParent", nodes[i].target);
                    //判读是否是子节点
                    var isLeft = $("#tree").tree("isLeaf", nodes[i].target);
                    if (isLeft) {
                        if (nodes[i].id != null) {
                            if (!actions[parentNode.id]) {
                                actions[parentNode.id] = [];
                            }
                            actions[parentNode.id].push(nodes[i].id);

                        } else {
                            actions.menuid = parentNode.id;
                            actions.actionid = 0;
                        }
                    }
                }
                for (var k in actions) {
                    var item = { MenuID: k, ActionIDs: actions[k].join(',') };
                    arr.push(item);
                }
                postData.data = JSON.stringify(arr);
            }            

            AR.Utility.Ajax.post("AddPromission", "Sys_Menu", postData,
                    function (data) {
                        $.messager.progress('close');
                        AR.Utility.Window.showMsg(data.msg);
                    });

        })

        
        AR.Utility.Ajax.post("GetMenuAndAction", "Sys_Menu", { RoleID: roleID },
         function (data) {
             $("#tree").tree({
                 idField: "menuid",
                 textField: "menuname",
                 rootText: "菜单根目录",
                 rootID: undefined,
                 checkbox: true,
                 parentField: "parentmenuid",
                 iconCls: 'icon-mini-add',
                 data: data,
                 onLoadSuccess: function (data) {
                     getActionIds();
                 }

             });
         }
    );

    })

    function getActionIds() {
        AR.Utility.Ajax.post("GetMenuIDsandActionIds", "", { RoleID: roleID },
         function (data) {
             for (var i in data) {
                 var node = $("#tree").tree("find", i);
                 if (node != null) {
                     var action = data[i].toLocaleLowerCase().split(',');
                     var parentId = node.id;
                     var childen = $("#tree").tree("getChildren", node.target);
                     for (var n = 0; n < action.length; n++) {
                         for (var j = 0; j < childen.length; j++) {
                             if (childen[j].attributes.parentmenuid.toString().toLocaleLowerCase() == parentId.toString()) {
                                 if (childen[j].id.toString().toLocaleLowerCase() == action[n].toString())
                                     $("#tree").tree("check", childen[j].target)
                             }
                         }
                     }
                 }
             }
         })
    }

</script>